import json
import time

import requests
from lxml import etree

from functools import partial
import subprocess

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')

import execjs

session = requests.Session()

headers = {
    "Accept": "*/*",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Origin": "https://www.ouyeel.com",
    "Referer": "https://www.ouyeel.com/search-ng/queryResource/index",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
}
list_url = "https://www.ouyeel.com/search-ng/commoditySearch/queryCommodityResult"


def fetch_1():
    response = session.post(list_url, headers=headers)
    # print("fetch_1:::", response.text)
    tree = etree.HTML(response.text)
    meta_content = tree.xpath('//meta[2]/@content')[0]
    ts_js = tree.xpath('//script[1]/text()')[0]
    auto_script_url = 'https://www.ouyeel.com' + tree.xpath('//script[@src]/@src')[0]
    auto_script_js = session.get(auto_script_url).text

    return meta_content, ts_js, auto_script_js


def generate_cookie(meta_content, ts_js, auto_script_js):
    with open("04 main.js", encoding='utf-8') as fp:
        jsCode = fp.read()
    with open("01 env.js", encoding='utf-8') as fp:
        envCode = fp.read()
    envCode.replace("meta_content", meta_content).replace("'ts_js'", ts_js)
    jsCode = jsCode.replace("meta_content", meta_content).replace("'ts_js'", ts_js).replace("'auto_script_js'", auto_script_js)
    JS = execjs.compile(jsCode)
    js_cookie = JS.call("get_cookies")
    cookie_key_value = js_cookie.split(";")[0].split("=")
    session.cookies.update({cookie_key_value[0]: cookie_key_value[1]})


def fetch_2(page_index):
    data = {
        "criteriaJson": '{"pageIndex":%d,"pageSize":50,"industryComponent":null,"channel":null,"productType":null,"sort":null,"warehouseCode":null,"key_search":null,"is_central":null,"searchField":null,"companyCode":null,"inquiryCategory":null,"inquirySpec":null,"provider":null,"packCodes":null,"shopCode":null,"steelFactory":null,"resourceIds":null,"jsonParam":{},"excludeShowSoldOut":null}'%(page_index)
    }
    response = session.post(list_url, headers=headers, data=data)
    # print("产品列表:::", response, response.text)
    ret_list_str = response.json().get("resultList")
    ret_list = json.loads(ret_list_str)
    for item in ret_list:
        print("item:::", item)

def main():
    for i in range(0, 1):
        meta_content, ts_js, auto_script_js = fetch_1()
        # print(session.cookies.get_dict())
        generate_cookie(meta_content, ts_js, auto_script_js)
        print(i, session.cookies.get_dict())
        fetch_2(i)
        session.cookies.clear()
        time.sleep(1)


if __name__ == '__main__':
    main()
    session.close()

